<template>
    <div>
        <table>
            <tr>
                <td>id</td>
                <td>分类</td>
                <td>修改</td>
                <td>删除</td>
            </tr>
            <tr v-for="i in frm">
                <td>{{i.id}}</td>
                <td>{{i.name}}</td>
                <td>
                    <van-button @click="show__s(i.id)">修改</van-button>
                </td>
                <td>
                    <van-button @click="del(i.id)">删除</van-button>
                </td>
            </tr>
        </table>
         <van-button @click="show_s" class="tianjia">添加</van-button>
            <!-- 修改 v-show是隐藏 -->
            <div v-show="show"> 
                        名字<input type="text" v-model="name">
                        <van-button @click="put_cate">修改</van-button>
            </div>

            <!-- 添加 -->
             <div v-show="show_add"> 
                              <van-field v-model="from.name" label="名称"  placeholder="请输入用户名" />

                                <van-button @click="cate_add" class="tanjia11">添加</van-button>
            </div>
    </div>
</template>

<script>
    export default {
        data(){
            return{
                frm:{},
                name:"",
                show:0,
                id:0,
                show_add:0,
                from:{}
            }
        },
        methods:{
            show_s(){
                this.show_add=1
            },
                cate_add(){
                this.$axios.post('cateM/',this.from).then(res=>{
                    if(res.data.code==200){
                        alert('添加成功')
                        this.show_add=0
                        this.get_cate()
                    }
                })
            },
            show__s(id){
                this.show=1
                this.id=id
            },
            get_cate(){
                this.$axios.get('cateM/').then(res=>{
                    this.frm = res.data.list
                })
            },
            put_cate(){
                this.$axios.put('cateM/?id='+this.id+'&name='+this.name) 
                .then(res=>{
                       console.log(res.data) 
                       this.get_cate()

                })

                this.show=0
            },
            del(id){
                this.$axios.delete('cateM/?id='+id)
                .then(res=>{
                     console.log(res.data)
                     this.get_cate()
                })
            }
        },
        mounted(){
            this.get_cate()
        }
    }
</script>

<style scoped>

</style>